// pages/wallet/index.js
const date = new Date()
const nowYear = date.getFullYear()
const nowMonth = date.getMonth() + 1
const nowDay = date.getDate()
const format = require('../../utils/format.js')
let daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
// 根据年月 获取当月的总天数
let getDays = function(year, month) {
    if (month === 2) {
        return ((year % 4 === 0) && ((year % 100) !== 0)) || (year % 400 === 0) ? 29 : 28
    } else {
        return daysInMonth[month - 1]
    }
}
// 根据年月日设置当前月有多少天 并更新年月日数组
let setDate = function(year, month, day, _th) {
    let daysNum = year === nowYear && month === nowMonth ? nowDay : getDays(year, month)
    day = day > daysNum ? 1 : day
    let monthsNum = year === nowYear ? nowMonth : 12
    let years = []
    let months = []
    let days = []
    let yearIdx = 9999
    let monthIdx = 0
    let dayIdx = 0

    // 重新设置年份列表
    for (let i = 2010; i <= nowYear; i++) {
        years.push(i)
    }
    years.map((v, idx) => {
        if (v === year) {
            yearIdx = idx
        }
    })
    // 重新设置月份列表
    for (let i = 1; i <= monthsNum; i++) {
        months.push(i)
    }
    months.map((v, idx) => {
        if (v === month) {
            monthIdx = idx
        }
    })
    // 重新设置日期列表
    for (let i = 1; i <= daysNum; i++) {
        days.push(i)
    }
    days.map((v, idx) => {
        if (v === day) {
            dayIdx = idx
        }
    })

    console.log(day, 'setData')
    _th.setData({
        years: years, //年份列表
        months: months, //月份列表
        days: days, //日期列表
        value: [yearIdx, monthIdx, dayIdx],
        year: year,
        month: month,
        day: day
    })
}
Page({

    /**
     * 页面的初始数据
     */
    data: {
        years: [],
        months: [],
        days: [],
        year: nowYear,
        month: nowMonth,
        day: nowDay,
        value: [9999, 1, 1],
        showDistpicker: false,
        balance: 0,
        type_index: 0,
        bill_type_arr: ["全部", "收益", "提现", "消费", "充值", "退款"],
        bill_type_obj: {
            "1": "收益",
            "2": "提现",
            "3": "消费",
            "4": "充值",
            "5": "退款",
        },
        bill_type_bgcolor: {
            "1": "#009966",
            "2": "#FF6600",
            "3": "#3399CC",
            "4": "#FFCC00",
            "5": "#B57795",
        },
        //账单状态
        bill_status_obj: {
            "1": "审核中",
            "-1": "审核未通过",
            "2": "审核通过",
            "3": "成功",
            "4": "成功",
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        setDate(this.data.year, this.data.month, this.data.day, this)
        this.loadBalance()
        this.loadBill()
    },
    loadBalance() {
        wx.showLoading({
            title: '加载中...',
        })
        getApp().http.post('account/info').then(res => {
            this.setData({
				balance: res.data.recharge_balance ? res.data.recharge_balance : 0,
            })
            wx.hideLoading()
        }).catch(err => {
            wx.hideLoading();
        })
    },
    loadBill() {
        let {
            page = 1, nextPage = true, loading = false, list = [], year = null, month = null, type_index = 0
        } = this.data
        if (!nextPage || loading) return
        this.setData({
            loading: true
        })
        getApp().http.get('account/flow-list', {
            data: {
                y: year,
                m: month,
                page,
                category: type_index == 0 ? '' : type_index
            }
        }).then(res => {
            page++
            nextPage = res.data.next_page
            loading = false
            list.push(...res.data.list.map(o => {
                let t = new Date(o.created_at * 1000)
                o.month_day = format.dateformat.format(t, 'MM-dd')
                o.hour_minute = format.dateformat.format(t, 'hh:mm')
                o.income_or_pay = o.fact_amount.indexOf('-') == -1 ? true : false
                o.service_charge = (Math.abs(o.tally_amount * 100) - Math.abs(o.fact_amount * 100)) / 100
                return o
            }))
            this.setData({
                page,
                nextPage,
                loading,
                list
            })
            wx.stopPullDownRefresh()
        })
    },
    //选择交易类型
    bindPickerChange(e) {
        this.setData({
            type_index: e.detail.value,

        })
        this.billRefresh()
    },
    //弹出日期选择
    showDistpicker: function() {
        this.setData({
            showDistpicker: !this.data.showDistpicker
        })
    },
    bindChange: function(e) {
        let val = e.detail.value
        setDate(this.data.years[val[0]], this.data.months[val[1]], 1, this)
    },
    //修改日期确认
    dateConfirm() {
        this.showDistpicker()
        this.billRefresh()
    },
    billRefresh() {
        this.setData({
            page: 1,
            nextPage: true,
            loading: false,
            list: []
        });
        this.loadBill()
    },
    onPullDownRefresh: function(value) {
        this.loadBalance()
        this.billRefresh()
    },
    onReachBottom: function() {
        this.loadBill()
    },
})